package com.fpx;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TTTTTTTTTTTTTT {
    private static final String clazz = "oracle.jdbc.driver.OracleDriver";

    public static void main(String[] args) throws SQLException {
        String url = "jdbc:oracle:thin:@172.16.3.25:1522:orcle";
        String user = "oprt";
        String password = "oprtPwD7xms8zz";
        String url1 = "jdbc:oracle:thin:@172.16.3.103:1522:xmslog";
        String user1 = "blg";
        String password1 = "blg";
        Connection con = getConn(url, user, password);
        Connection con1 = getConn(url1, user1, password1);
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;

        String sql = "select mi_servehawbcode from bsn_mailitem where bs_id = ?";
        PreparedStatement ps = null;
        ResultSet rs = null;

        PreparedStatement ps11 = null;
        ResultSet rs11 = null;

        String sql1 = "SELECT cel_sendparameter FROM BLG.BL_MANIFESTLOG where bs_id= ? and mt_code='SP' and ML_FINISHSIGN='Y' order by ML_CREATEDATE ";
        PreparedStatement ps1 = null;
        ResultSet rs1 = null;
        Map<String, String> map1 = new HashMap<String, String>();
        try {
            String str = "";
            fis = new FileInputStream("d:\\aa.txt");
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            String sql11 = "select bs_id from tms_bagitem where bg_id =(select bg_id from tms_bag where BG_CODE=?)";
            while ((str = br.readLine()) != null) {
                str = str.trim();
                int errorCount=0;
                Map<String, String> map = new HashMap<String, String>();
                ps11 = con.prepareStatement(sql11);
                ps11.setString(1, str);
                rs11 = ps11.executeQuery();
                while (rs11.next()) {
                    String bs_id = rs11.getString(1);
                    ps = con.prepareStatement(sql);
                    ps.setString(1, bs_id);
                    rs = ps.executeQuery();
                   
                    while (rs.next()) {
                        String code = rs.getString(1);
                        ps1 = con1.prepareStatement(sql1);
                        ps1.setString(1, bs_id);
                        rs1 = ps1.executeQuery();
                        int count=0;
                        while (rs1.next()) {
                            count++;
                            String xml = rs1.getString(1);
                            // System.out.println(str+"::"+bs_id);
                            // System.out.println(xml);
                            String a1[] = xml.split("<packageCode>");
                            String a2[] = a1[1].split("</packageCode>");
                            if(!a2[0].equalsIgnoreCase(str)){
                                errorCount++; 
                                a2[0]=a2[0]+",error";
                            }
                            map.put(code, a2[0]);
                            //System.out.println(str + "," + code + "," + a2[0]);
                            break;
                        }
                        map1.put(str, String.valueOf(count));
                        rs1.close();
                        ps1.close();
                    }
                  
                    rs.close();
                    ps.close();
                }
                rs11.close();
                ps11.close();
                writeFile(str, map,errorCount);
            }
        } catch (FileNotFoundException e) {
            System.out.println("找不到指定文件");
        } catch (IOException e) {
            System.out.println("读取文件失败");
        } finally {
            try {
                br.close();
                isr.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            con1.close();
        }
        System.out.println(map1);
    }

    private static void writeFile(String fileName, Map<String, String> map,int errorCount) {
        String path = "D:\\AE\\";
        if(errorCount!=0){
            path="D:\\AE\\error\\";
        }
        FileWriter fw = null;
        try {
            fw = new FileWriter(path + fileName +"("+errorCount+"-"+map.keySet().size()+").txt");
            Set<Entry<String, String>> set = map.entrySet();
            for (Entry entry : set) {
                fw.write(entry.getKey() + "," + entry.getValue() + "\r\n");
            }
            if(map.keySet()==null){
                System.out.println(fileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
            }
        }

    }

    private static Connection getConn(String url, String user, String password) {
        Connection con = null;
        try {
            Class.forName(clazz);
            con = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

}
